/*
 * InternalFrame.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import com.cute4j.swing.graphics.Dimension;
import com.cute4j.swing.layouts.LayoutManager;

/**
 * InternalFrame Component.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class InternalFrame extends Window {

// Static Initialization. ------------------------------------------------------

  static {
    _initialize();
  }

// Private Fields. -------------------------------------------------------------

  private Dimension layoutPreferredSize;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new InternalFrame object.
   *
   * @param title the frame title
   */
  public InternalFrame(String title) {
    super(title);
  }

  /**
   * Constructs new InternalFrame object.
   *
   * @param layoutManager the layout manager
   * @param title the frame title
   */
  public InternalFrame(LayoutManager layoutManager, String title) {
    super(layoutManager, title);
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public Dimension getPreferredSize() {
    if (!preferredSizeSet && (!valid || (preferredSize == null))) {
      disposedCheck();
      preferredSize = _getPreferredSize(_delegate);
      if (insets != null) {
        preferredSize = preferredSize.enlarge(insets.getLeft() +
                                              insets.getRight(),
                                              insets.getTop() +
                                              insets.getBottom());
      }
    }
    return preferredSize;
  }

  void clearCache() {
    layoutPreferredSize = null;
    super.clearCache();
  }

// Native Callbacks. -----------------------------------------------------------

  Dimension _getLayoutPreferredSize() {
    if (!valid || (layoutPreferredSize == null)) {
      if (layoutManager == null) {
        layoutPreferredSize = Dimension.ZERO_DIMESNION;
      } else {
        layoutPreferredSize = layoutManager.preferredLayoutSize(this);
      }
    }
    return layoutPreferredSize;
  }

// Package-Private Native Methods. ---------------------------------------------

  native long _create();

  native void _insertChild(long delegate, long childDelegate, int index);

// Private Static Native Methods. ----------------------------------------------

  private static native void _initialize();

}
